package com.example.juyuanapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.juyuanapp.bean.BookBean;
import com.example.juyuanapp.bean.DetailBean;
import com.google.gson.Gson;

import org.json.JSONObject;

public class DetailActivity extends AppCompatActivity {
    TextView foodName,foodprice,foodlabel;
    int foodId;
    String name;
    String price;
    String label;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        initview();
        //获得点击的id
        Intent intent=getIntent();
        foodId = intent.getIntExtra("foodId",0)+1;
        show();
    }

    private void initview() {
        foodName = findViewById(R.id.foodname);
        foodprice = findViewById(R.id.price);
        foodlabel = findViewById(R.id.label);
    }

    private void show() {
        RequestQueue queue= Volley.newRequestQueue(DetailActivity.this);
        String url="http://192.168.43.251:8080/food/getFood?id=";
        JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, url+foodId,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Gson gson=new Gson();
                        DetailBean detailBean = gson.fromJson(jsonObject.toString(),DetailBean.class);
                        name=detailBean.getList().getName().trim();
                        price=detailBean.getList().getPrice().trim();
                        label = detailBean.getList().getLabel().trim();
                        if (detailBean.isSuccess()){
                            foodName.setText(name);
                            foodprice.setText(price);
                            foodlabel.setText(label);
                        }
                        Toast.makeText(DetailActivity.this,"id:"+foodId,Toast.LENGTH_SHORT).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(DetailActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
                    }
                });
        queue.add(request);
    }
}

